<template>
  <div id="home">
    <nav-bar font-color="white" class="home-bar">
      <div slot="center">小余购物</div>
    </nav-bar>
    <TabControl :titles="['流行','新款','精选']"
                ref="tabControl1"
                @tabClick="tabClick"
                class="top-control"
                v-show="isShowTopControl"/>
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @imgItemLoad="imgItemLoad"/>
      <recommend :recommend="recommend" />
      <feature-view />
      <TabControl :titles="['流行','新款','精选']"
                  ref="tabControl2"
                  @tabClick="tabClick" />
      <goods-list :goods="showGoods" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
  </div>
</template>

<script>
  //方法
  import {getHomeMultidata,getHomeGoods} from "@/network/home";
  import {itemImgLoad,backTopMixin} from "@/common/mixin";
  //公共组件
  import NavBar from "@/components/common/navbar/NavBar";
  import TabControl from "@/components/content/tabControl/TabControl";
  import GoodsList from "@/components/content/goods/GoodsList";
  import Scroll from "@/components/common/scroll/Scroll";

  //子组件
  import recommend from "@/views/home/childComps/recommend";
  import homeSwiper from "./childComps/homeSwiper";
  import FeatureView from "./childComps/FeatureView";

  export default {
    name: "home",
    components:{
      NavBar,
      homeSwiper,
      recommend,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll
    },
    data(){
      return {
        banners:[],
        recommend:[],
        goods: {
          'pop': {page: 0,list:[]},
          'new': {page: 0,list:[]},
          'sell': {page: 0,list:[]}
        },
        currentType: 'pop',
        topOffsetTop: 0,
        isShowTopControl: false,
        saveY: 0
      }
    },
    mixins:[itemImgLoad,backTopMixin],
    computed:{
      showGoods(){
        return this.goods[this.currentType].list
      }
    },
    activated() {
      this.$refs.scroll.refresh()
      this.$refs.scroll.scrollTo(0,this.saveY,0)
    },
    deactivated(){
      this.saveY = this.$refs.scroll.getScrollY()
      this.$bus.$off('itemImgLoad',this.itemListListener)
    },
    created() {
      //请求多个数据
      this.getHomeMultidata();
      //请求商品数据
      this.getHomeGoods('pop');
      this.getHomeGoods('new');
      this.getHomeGoods('sell');
    },

    methods:{
      imgItemLoad(){
        this.topOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      },
      loadMore(){
        this.getHomeGoods(this.currentType);
      },
      tabClick(index){
        switch (index){
          case 0:
            this.currentType = 'pop'
            break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        this.$refs.tabControl1.currentIndex = index;
        this.$refs.tabControl2.currentIndex = index;
      },

      contentScroll(position){
        this.listenShowBackTop(position,3000);
        if(-position.y >= this.topOffsetTop){
          this.isShowTopControl = true;
        }else {
          this.isShowTopControl = false;
        }

      },
      getHomeMultidata(){
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list;
          this.recommend = res.data.recommend.list;
        })
      },
      getHomeGoods(type){
        const page = this.goods[type].page + 1;
        getHomeGoods(type,page).then(res => {
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1;
          setTimeout(() => {
            this.$refs.scroll.finishPullUp();
          },2000);
        })
      }
    }
  }
</script>

<style scoped>
  #home{
    height: 100vh;
    position: relative;
  }
  .home-bar {
    z-index: 8;
  }
  .top-control{
    position: relative;
    z-index: 9;
  }
  ul{
    list-style-type: none;
    padding: 0;
    margin: 0;
  }
  .content{
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
    overflow: hidden;
  }
</style>
